<?php
class serverModel extends model
{
    public static $instance;
    public $errors = array();

    public function __construct()
    {
        parent::__construct();
    }


    /**
     * Get server pairs.
     *
     * @param  string $mode     all|noclosed or empty
     * @access public
     * @return array
     */
    public function findAll($mode = '')
    {
        if(defined('TUTORIAL')) return $this->loadModel('tutorial')->getProjectPairs();
        /* Order by status's content whether or not done */
        $servers = $this->dao->select('*')->from(TABLE_SERVER)
            ->where('status')->eq(1)
            ->fetchAll();

        return $servers;
    }

    /**
     * 模拟get进行url请求
     * @param string $url
     * @param array $post_data
     */
    public function get($url = '', $post_data = array())
    {
        if (empty($url)) {
            return false;
        }
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $request_data = substr($o, 0, -1);
        if($request_data != ""){
            $url = $url . (strpos($url, "?") > -1 ? "&" :"?") . $request_data;
        }
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        if(!$data){
            return null;
        }

        $result = json_decode($data, true);
        if(!$result){
            $result = array("content" => $data);
        }
        return $result;
    }

}